package com.itheima.mapperDome;

import com.itheima.mapperDome.mapper.LibraryMapper;
import com.itheima.pojo.Library;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//第二种方法  mapper
public class MapperTest {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();
        LibraryMapper libraryMapper = sqlSession.getMapper(LibraryMapper.class);
        List<Library> list = libraryMapper.selectAllLibrary();
        System.out.println(list);
        sqlSession.close();
    }

    @Test
    public void testSelectById() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();
        LibraryMapper libraryMapper = sqlSession.getMapper(LibraryMapper.class);
        Library library = libraryMapper.selectLibraryById(1);
        System.out.println(library);
        sqlSession.close();
    }

    @Test
    public void selectByCondition() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();
        LibraryMapper libraryMapper = sqlSession.getMapper(LibraryMapper.class);

        Map map = new HashMap();
        map.put("title", "%新%");
        map.put("min", 7);
        map.put("max", 15);
        List<Library> list = libraryMapper.selectByCondition(map);
        System.out.println(list);
        sqlSession.close();
    }

    @Test
    public void addLibrary() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 自动提交事务
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        LibraryMapper libraryMapper = sqlSession.getMapper(LibraryMapper.class);
        Library library = new Library();
//        library.setId(10);
        library.setMomey(80);
        library.setTitle("haha");
        library.setNumber(11);

        libraryMapper.insertLibrary(library);
//        sqlSession.commit();

                System.out.println(library.getId());
        sqlSession.close();
    }

    @Test
    public void updateLibrary() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 自动提交事务
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        LibraryMapper libraryMapper = sqlSession.getMapper(LibraryMapper.class);
        Library library = new Library();
        library.setId(4);
        library.setMomey(80);
        library.setTitle("修改后名称2");
        library.setNumber(11);
        int i = libraryMapper.updateLibrary(library);
//        sqlSession.commit();
        System.out.println(i);
        sqlSession.close();
    }

    @Test
    public void deleteLibrary() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 自动提交事务
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        LibraryMapper libraryMapper = sqlSession.getMapper(LibraryMapper.class);
        libraryMapper.deleteLibraryById(4);
        sqlSession.close();
    }

    @Test
    public void deleteByIds() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 自动提交事务
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        LibraryMapper libraryMapper = sqlSession.getMapper(LibraryMapper.class);
        int[] ids = {5,6,7};
        libraryMapper.deleteByIds(ids);
        sqlSession.close();
    }

    @Test
    public void testSelectByCon() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();
        LibraryMapper libraryMapper = sqlSession.getMapper(LibraryMapper.class);
        Library library = libraryMapper.selectLibraryByCon(1);
        System.out.println(library);
        sqlSession.close();
    }
}